"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : security-overflow
Case Name   : 执行 CREATE USER MAPPING，测试指定过长的 password 是否会造成堆栈溢出而导致拒绝服务
Create At   : 2023/06/07
@zou_jialiang0501162244
Description :
    1. 创建 postgres_fdw 扩展
    2. 创建外部服务器
    3. 创建用户映射，password 参数超长
    4. 清理环境
Expect      :
    1. 成功
    2. 成功
    3. 失败
    4. 成功
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class SecurityDesign(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.cons = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.server = 'ser_security_0002'

    def test_security(self):
        text1 = '-----step1:创建 postgres_fdw 扩展;expect:成功-----'
        self.log.info(text1)
        msg = self.pri_sh.execut_db_sql(
            'create extension if not exists postgres_fdw;')
        self.log.info(msg)
        self.assertIn(self.cons.create_extension_success, msg, f'执行失败:{text1}')

        text2 = '-----step2:创建外部服务器;expect:成功-----'
        self.log.info(text2)
        msg = self.pri_sh.execut_db_sql(
            f"drop server if exists {self.server};"
            f"create server {self.server} foreign data wrapper postgres_fdw "
            f"options(host '{self.pri_node.db_host}', "
            f"port '{self.pri_node.db_port}', "
            f"dbname '{self.pri_node.db_name}');")
        self.log.info(msg)
        self.assertIn(self.cons.create_server_success, msg, f'执行失败：{text2}')

        text3 = '-----step3:创建用户映射，password 参数超长;expect:报错-----'
        self.log.info(text3)
        password = 'a' * 16000
        res = self.pri_sh.execute_generate('default', prefix='usermapping')
        self.assertIn(self.cons.create_keycipher_success, res, f'执行失败:{text3}')
        msg = self.pri_sh.execut_db_sql(
            f"create user mapping for current_user server {self.server} "
            f"options(user 'postgres', password '{password}');")
        self.log.info(msg)
        self.assertIn('ERROR', msg, f'执行失败：{text3}')

    def tearDown(self):
        text4 = '-----step4:清理数据;expect:成功-----'
        self.log.info(text4)
        msg = self.pri_sh.execut_db_sql(f"drop server {self.server} cascade;"
                                        f"drop extension postgres_fdw;")
        self.log.info(msg)
        self.assertIn(self.cons.drop_server_success, msg, f"执行失败:{text4}")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
